﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceBlender.DAL;
using ResourceBlender.Core;
using ResourceBlender.Express.Properties;

namespace ResourceBlender.Express {
    /// <summary>
    /// Holds configuration data for ResourceBlender Express.
    /// </summary>
    public class WPFConfiguration : IDataConfiguration, IConfiguration {
        private DatabaseType _dbMode;
        private string _dbHost = string.Empty,
                       _dbDatabase = string.Empty,
                       _tablePrefix = string.Empty,
                       _dbUser = string.Empty,
                       _dbPass = string.Empty,
                       _dbPath = string.Empty,
                       _salt = string.Empty;
        private int _dbPort = -1;

        #region IDataConfiguration
        public DatabaseType DBMode {
            get { return _dbMode; }
            set { _dbMode = value; }
        }

        public string DBHost {
            get { return _dbHost; }
            set { _dbHost = value; }
        }

        public int DBPort {
            get { return _dbPort; }
            set { _dbPort = value; }
        }

        public string DBDatabase {
            get { return _dbDatabase; }
            set { _dbDatabase = value; }
        }

        public string TablePrefix {
            get { return _tablePrefix; }
            set { _tablePrefix = value; }
        }

        public string DBUser {
            get { return _dbUser; }
            set { _dbUser = value; }
        }

        public string DBPassword {
            get { return _dbPass; }
            set { _dbPass = value; }
        }

        public string DBPath {
            get { return _dbPath; }
            set { _dbPath = value; }
        }
        #endregion

        #region IConfiguration
        /// <summary>
        /// Gets or sets the salt used to secure passwords.
        /// </summary>
        /// <value>The salt used to secure passwords.</value>
        public string Salt {
            get { return _salt;}
            set { _salt = value; }
        }
        #endregion

        /// <summary>
        /// Initializes a new <see cref="WPFConfiguration"/> for SQLite.
        /// </summary>
        /// <param name="dbPath">The SQLite database filename.</param>
        /// <param name="tablePrefix">The table prefix.</param>
        public WPFConfiguration(string dbPath, string tablePrefix) {
            _dbMode = DatabaseType.SQLite;
            _dbPath = dbPath;
            _tablePrefix = tablePrefix;
        }

        /// <summary>
        /// Initializes a new <see cref="WPFConfiguration"/>.
        /// </summary>
        /// <param name="dbMode">The database mode.</param>
        /// <param name="dbHost">The database hostname.</param>
        /// <param name="dbPort">The database port.</param>
        /// <param name="dbDatabase">The name of the database on the server.</param>
        /// <param name="tablePrefix">The table prefix.</param>
        /// <param name="dbUser">The database username.</param>
        /// <param name="dbPass">The database password.</param>
        public WPFConfiguration(DatabaseType dbMode, string dbHost, int dbPort, string dbDatabase, string tablePrefix, string dbUser, string dbPass) {
            _dbMode = dbMode;
            _dbHost = dbHost;
            _dbPort = dbPort;
            _dbDatabase = dbDatabase;
            _tablePrefix = tablePrefix;
            _dbUser = dbUser;
            _dbPass = dbPass;
        }

        /// <summary>
        /// Loads configuration data from The Properties.Settings configuration.
        /// </summary>
        internal static void LoadConfig() {
            DatabaseType dbType = (DatabaseType)Enum.Parse(typeof(DatabaseType), Settings.Default["DBMode"].ToString());
            string dbHost = Settings.Default["DBHost"].ToString(),
                   dbDatabase = Settings.Default["DBDatabase"].ToString(),
                   dbUser = Settings.Default["DBUser"].ToString(),
                   dbPass = Settings.Default["DBPassword"].ToString(),
                   dbPath = Settings.Default["DBPath"].ToString(),
                   dbTablePrefix = Settings.Default["DBTablePrefix"].ToString();
            int dbPort = int.Parse((Settings.Default["DBPort"] ?? "-1").ToString());

            WPFConfiguration config;
            if(dbType == DatabaseType.SQLite) {
                config = new WPFConfiguration(dbPath, dbTablePrefix);
            } else {
                config = new WPFConfiguration(dbType, dbHost, dbPort, dbDatabase, dbTablePrefix, dbUser, dbPass);
            }
            DalConfigurationManager.Configure(config);

            config.Salt = Settings.Default["salt"].ToString();
            CoreConfigurationManager.Configure(config);
        }
    }
}
